package com.engine.modules.vector.mapper;

import com.engine.init.Globle;
import com.engine.init.entity.TableToGroupEntity;
import com.engine.common.baseEntity.gis.BoxRangeEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
@Slf4j
public class VectorInfoMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    public Map<String,Object> selectTableInfo(String tableName){
        String sql="SELECT pp.tablename,gg.srid,gg.type,gg.coord_dimension,cc.comment FROM pg_tables AS pp LEFT JOIN " +
                "geometry_columns AS gg ON pp.tablename=gg.f_table_name LEFT JOIN (select relname as tabname," +
                "obj_description(relname::regclass, 'pg_class') as comment from pg_class c where  relkind = 'r' and " +
                "relname not like 'pg_%' and relname not like 'sql_%' order by relname ) AS cc ON pp.tablename=cc" +
                ".tabname WHERE pp.schemaname='public' AND pp.tablename ='"+tableName+"'";
        Map<String,Object> tableInfo = jdbcTemplate.queryForMap(sql);
        sql="SELECT A.attname as name,format_type(a.atttypid,a.atttypmod) as type,col_description(a.attrelid,a" +
                ".attnum) as comment FROM pg_catalog.pg_attribute A WHERE A.attrelid = ( SELECT oid FROM pg_class " +
                "WHERE relname = '"+tableName+"' )  AND A.attnum > 0  AND NOT A.attisdropped ORDER BY A.attnum";
        List<Map<String,Object>> columnsInfo = jdbcTemplate.queryForList(sql);
        Map<String,Object> info = new HashMap<>();
        info.put("table_name",tableName);
        info.put("table_info",tableInfo);
        info.put("columns_info",columnsInfo);
        return info;
    }

    public Map<String, Object> selectTableStyle(String tableName) {
        if (Globle.TablePropertiesMap.get(tableName) == null)
            throw new NullPointerException("tableName="+tableName+",数据库中表不存在或未添加进数据目录");
        Map<String, Object> map=new HashMap<>();
        map.put("SourceName", Globle.TablePropertiesMap.get(tableName).getTableName());
        map.put("LayerName", Globle.TablePropertiesMap.get(tableName).getTableName());
        map.put("SourceStyle", Globle.TablePropertiesMap.get(tableName).getSourceStyle());
        map.put("LayerStyle", Globle.TablePropertiesMap.get(tableName).getLayerStyle());
        return map;
    }

    public Map<String, Object> selectGroupStyle(String groupName) {
        if (Globle.GroupPropertiesMap.get(groupName) == null)
            throw new NullPointerException("groupName="+groupName+",数据库中表不存在或未添加进数据目录");
        Map<String, Object> map=new HashMap<>();
        map.put("SourceName", Globle.GroupPropertiesMap.get(groupName));
        map.put("SourceStyle", Globle.GroupPropertiesMap.get(groupName).getGroupStyle());
        Map<String,String> maps=new HashMap<>();
        for (TableToGroupEntity ttge: Globle.GroupPropertiesMap.get(groupName).getListTableToGroupEntity()) {
            maps.put("LayerName",ttge.getLayerName());
            maps.put("LayerStyle",ttge.getStyle());
        }
        return map;
    }
}
